package com.foldingleaf.web.controller.withdraw;

import com.foldingleaf.common.annotation.Log;
import com.foldingleaf.common.core.controller.BaseController;
import com.foldingleaf.common.core.domain.Result;
import com.foldingleaf.common.core.domain.entity.SysUser;
import com.foldingleaf.common.core.page.TableDataInfo;
import com.foldingleaf.common.enums.BusinessType;
import com.foldingleaf.common.utils.poi.ExcelUtil;
import com.foldingleaf.system.service.impl.SysUserServiceImpl;
import com.foldingleaf.withdraw.domain.WithdrawRecord;
import com.foldingleaf.withdraw.service.IWithdrawRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 提现记录Controller
 * 
 * @author ajian
 * @date 2022-06-22
 */
@RestController
@RequestMapping("/withdraw/withdraw")
public class WithdrawRecordController extends BaseController
{
    @Autowired
    private IWithdrawRecordService withdrawRecordService;
    @Autowired
    private SysUserServiceImpl sysUserService;
    /**
     * 查询提现记录列表
     */

    @GetMapping("/list")
    public TableDataInfo list(WithdrawRecord withdrawRecord)
    {
        startPage();
        List<WithdrawRecord> list = withdrawRecordService.selectWithdrawRecordList(withdrawRecord);
        return getDataTable(list);
    }

    /**
     * 导出提现记录列表
     */
    @PreAuthorize("@ss.hasPermi('withdraw:withdraw:export')")
    @Log(title = "提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WithdrawRecord withdrawRecord)
    {
        List<WithdrawRecord> list = withdrawRecordService.selectWithdrawRecordList(withdrawRecord);
        ExcelUtil<WithdrawRecord> util = new ExcelUtil<WithdrawRecord>(WithdrawRecord.class);
        util.exportExcel(response, list, "提现记录数据");
    }

    /**
     * 获取提现记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('withdraw:withdraw:query')")
    @GetMapping(value = "/{recordId}")
    public Result getInfo(@PathVariable("recordId") Long recordId)
    {
        return Result.success(withdrawRecordService.selectWithdrawRecordByRecordId(recordId));
    }

    /**
     * 新增提现记录
     */

    @Log(title = "提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public Result add(@RequestBody WithdrawRecord withdrawRecord)
    {
        try {
            if (withdrawRecord.getAmount() == null) {
                return Result.error("金额不能为空");
            }
            //提现金额不能小于30
            if (withdrawRecord.getAmount().compareTo(new BigDecimal(30)) < 0) {
                return Result.error("提现金额不能小于30");
            }
            SysUser sysUser = sysUserService.selectUserById(withdrawRecord.getUserId());
            if (sysUser == null) {
                return Result.error(401,"获取用户信息异常");
            }
            if (sysUser.getBalance().compareTo(withdrawRecord.getAmount()) < 0) {
                return Result.error("余额不足");
            }
            //减少用户余额并添加到冻结余额
            sysUser.setBalance(sysUser.getBalance().subtract(withdrawRecord.getAmount()));
            sysUser.setFrozenBalance(sysUser.getFrozenBalance().add(withdrawRecord.getAmount()));
            sysUserService.updateUser(sysUser);

        }catch (Exception e){
            logger.error("新增提现记录失败", e);
            return Result.error("提现失败");
        }

        return toAjax(withdrawRecordService.insertWithdrawRecord(withdrawRecord));
    }

    /**
     * 修改提现记录
     */
    @PreAuthorize("@ss.hasPermi('withdraw:withdraw:edit')")
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody WithdrawRecord withdrawRecord)
    {
        return toAjax(withdrawRecordService.updateWithdrawRecord(withdrawRecord));
    }

    /**
     * 删除提现记录
     */
    @PreAuthorize("@ss.hasPermi('withdraw:withdraw:remove')")
    @Log(title = "提现记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public Result remove(@PathVariable Long[] recordIds)
    {
        return toAjax(withdrawRecordService.deleteWithdrawRecordByRecordIds(recordIds));
    }
}
